<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Db;

/**
 *  语音通话
 */
class VoiceCall extends CallBase
{
    protected $table = 'voice_call';
    protected $fillable = [
        'user_id', 'chat_user_id', 'status', 'from', 'start_time', 'coin_setting', 'end_time', 'call_mark', 'read'
    ];

    //通话类型
    const CALL_TYPE = 0;

    /**
     * 新手福利免费语聊当前总时间
     */
    static function totalFreeMinutes($userId = 0)
    {
        $where = [
            ['user_id', $userId],
            ['from', '=', self::FROM_NEW]
        ];
        $logs = self::where($where)->get()->toArray();
        $minutes = 0;
        foreach ($logs as $log) {
            if ($log['end_time'] && $log['call_time']) {
                $minutes += ceil($log['call_time'] / 60);
            } elseif ($log['status'] == self::STATUS_ING && $log['answer_time']) {
                $minutes += ceil((time() - $log['answer_time']) / 60);
            }
        }
        return $minutes;
    }

    /**
     * 接听过免费电话的记录
     */
    static function getCallRecords($fromNew = false)
    {
        $where = [
            ['answer_time', '>', 0],
        ];
        if ($fromNew) $where[] = ['from', '=', self::FROM_NEW];
        else $where[] = ['from', '<>', self::FROM_NEW];
        $excludeStatus = [self::STATUS_INVALID, self::STATUS_NO_PICK];
        return self::where($where)->whereNotIn('status', $excludeStatus)->get()->toArray();
    }
}
